﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace jlinkdll
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ~Form1()
        {
            JlinkAPI.JLINKARM_Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //LogComDelegate dele = new LogComDelegate(PrintLog);
            //JlinkAPI.JLINKARM_EnableLogCom(dele);
            try
            {
                int a = JlinkAPI.JLINK_GetSN();
                Console.WriteLine(a);
            }
            catch (Exception)
            {
                if (!JlinkAPI.JLINKARM_HasError())
                {
                    Console.WriteLine("\nOK\n");
                }
            }


            //JlinkAPI.JLINKARM_TIF_Select(1);//TIF = transport interface??? 1=swd???
            //JlinkAPI.JLINKARM_ExecCommand("device = STM32F103C8", 0, 4000, 0);
            //JlinkAPI.JLINKARM_SetSpeed(4000);

            //JlinkAPI.JLINKARM_Reset();
            //JlinkAPI.JLINKARM_Halt();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog op = new OpenFileDialog();
                if (op.ShowDialog() == DialogResult.OK)
                {
                    FlashBin(op.FileName);
                }

                JlinkAPI.JLINKARM_Reset();
                JlinkAPI.JLINKARM_Go();
                //JlinkAPI.JLINKARM_Halt();
                //byte[] buf = new byte[1024];
                //JLINKARM_GetFeatureString(buf);
                //MessageBox.Show(Marshal.PtrToStringAnsi(JLINKARM_GetCompileDateTime()));
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        public void PrintLog(StringBuilder str)
        {
            Console.WriteLine(str.ToString());
        }

        public UInt16 JLINKARM_ReadU16(UInt32 addr)
        {
            UInt16 dat = 0;
            byte stu = 0;
            JlinkAPI.JLINKARM_ReadMemU16(addr, 1, ref dat, ref stu);
            MessageBox.Show(dat.ToString());
            return dat;
        }

        public void FlashBin(string filename)
        {
            System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open);
            System.IO.BinaryReader read = new System.IO.BinaryReader(fs);
            if (!JlinkAPI.JLINKARM_IsHalted()) JlinkAPI.JLINKARM_Halt();
            JlinkAPI.JLINKARM_BeginDownload(0);
            JlinkAPI.JLINKARM_WriteMem(0x08000000, (UInt32)fs.Length, read.ReadBytes((int)fs.Length));
            JlinkAPI.JLINKARM_EndDownload();
            //uint a =0xaa ;
            //byte b = 0xbb;
            //JlinkAPI.JLINKARM_ReadMemU32(0x08000000, 1, ref a, ref b);
            read.Dispose();
            fs.Close();
            fs.Dispose();

        }
    }
}
